package acom.dhy.qigsaw2test.qigsaw

import acom.dhy.qigsaw2test.R
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.iqiyi.android.qigsaw.core.ObtainUserConfirmationDialog
import com.iqiyi.android.qigsaw.core.common.SplitLog
import java.text.DecimalFormat

class SampleObtainUserConfirmationDialog : ObtainUserConfirmationDialog() {
    private var fromUserClick = false
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (checkInternParametersIllegal()) {
            finish()
            return
        }
        SplitLog.d(TAG, "Downloading splits %s need user to confirm.$moduleNames")
        setContentView(R.layout.activity_sample_obtain_user_confirmation)
        window.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        setFinishOnTouchOutside(false)
        val descText = findViewById<TextView>(R.id.sample_user_conformation_tv)
        val df = DecimalFormat("#.00")
        val convert = (realTotalBytesNeedToDownload / (1024f * 1024f)).toDouble()
        descText.text = String.format(getString(R.string.sample_prompt_desc), df.format(convert))
        findViewById<View>(R.id.sample_user_confirm).setOnClickListener {
            if (!fromUserClick) {
                fromUserClick = true
                onUserConfirm()
            }
        }
        findViewById<View>(R.id.sample_user_cancel).setOnClickListener {
            if (!fromUserClick) {
                fromUserClick = true
                onUserCancel()
            }
        }
    }

    companion object {
        private const val TAG = "SampleObtainUserConfirmationDialog"
    }
}